package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.TWorkOrder;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ICensusService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 数据分析
 */
@Service
public class CensusServiceImpl implements ICensusService {

    @Autowired
    private CensusMapper censusMapper;
    @Autowired
    private TWorkOrderMapper tWorkOrderMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TWorkOrderItemMapper tWorkOrderItemMapper;
    @Autowired
    private TAccessRecordsMapper tAccessRecordsMapper;
    @Autowired
    private TEfficAccessRecordsMapper tEfficAccessRecordsMapper;

    @Override
    public JSONObject groupList(boolean isAmin) {
        JSONObject json = new JSONObject();
        json.put("userNum",censusMapper.countByUser());
        json.put("workNum",censusMapper.countByWork());
        json.put("realmNum",censusMapper.countByRealm());
        json.put("floorNum",censusMapper.countByWorkItem());
        return json;
    }

    @Override
    public List<TWorkOrder> works(TWorkOrder tWorkOrder) {

        List<TWorkOrder> tWorkOrders = tWorkOrderMapper.selectTWorkOrderList(tWorkOrder);
        if (!tWorkOrders.isEmpty()){
            for (TWorkOrder workOrder : tWorkOrders) {
                workOrder.setSysUser(sysUserMapper.selectUserById(workOrder.getUid()));
                workOrder.setFloorNum(tWorkOrderItemMapper.countByWorkOrderId(workOrder.getId()));
            }
        }
        return tWorkOrders;
    }

    private static final String[] DAY_INFO = {"00点","01点","02点","03点","04点","05点","06点","07点","08点","09点","10点","11点","12点","12点","14点","15点","16点","17点","18点","19点","20点","21点","22点","23点"};

    @Override
    public JSONObject accessOfDay(Long id) {
        JSONObject jsonObject = new JSONObject();
        List<Date> list = getDayTimes();
        List<Long> y1 = new ArrayList<>();
        List<Long> y2 = new ArrayList<>();
        for (Date date : list) {
            y1.add(tAccessRecordsMapper.countBywWrkOrderIdAndTime(id,date,1));
            y2.add(tEfficAccessRecordsMapper.countBywWrkOrderIdAndTime(id,date,1));
        }
        jsonObject.put("x",DAY_INFO);
        jsonObject.put("y1",y1);
        jsonObject.put("y2",y2);
        return jsonObject;
    }

    @Override
    public JSONObject accessOfMonth(Long id) {
        JSONObject jsonObject = new JSONObject();
        List<Date> list = getMonthTimes(getMonthOfNum().size());
        List<Long> y1 = new ArrayList<>();
        List<Long> y2 = new ArrayList<>();
        for (Date date : list) {
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            y1.add(tAccessRecordsMapper.countBywWrkOrderIdAndTime(id,date,2));
            y2.add(tEfficAccessRecordsMapper.countBywWrkOrderIdAndTime(id,date,2));
        }
        jsonObject.put("x",getMonthOfNum());
        jsonObject.put("y1",y1);
        jsonObject.put("y2",y2);
        return jsonObject;
    }

    /**
     * 获取每天所有小时的时间集
     * @return 时间集
     */
    private List<Date> getDayTimes() {
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = df.format(day);
        List<Date> list = new ArrayList<Date>();
        try {
            Date date = df.parse(s);
            for (int i = 0; i < 24; i++) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                String s1 = format.format(date);
                list.add(format.parse(s1));
                cal.add(Calendar.HOUR, 1);
                date = cal.getTime();
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    /**
     * 获取当月天数
     * @return 天数
     */
    private List<String> getMonthOfNum(){
        YearMonth yearMonth = YearMonth.now();
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        int numOfDays = firstDayOfMonth.lengthOfMonth();
        System.err.println(numOfDays);
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= numOfDays; i++) {
            list.add(i+"号");
        }
        return list;
    }

    /**
     * 获取当月天数时间集
     * @param length 天数
     * @return 时间集
     */
    private List<Date> getMonthTimes(int length) {
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-00 00:00:00");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = df.format(day);
        List<Date> list = new ArrayList<Date>();
        try {
            Date date = df.parse(s);
            for (int i = 0; i < length; i++) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                String s1 = format.format(date);
                list.add(format.parse(s1));
                cal.add(Calendar.DAY_OF_MONTH, 1);
                date = cal.getTime();
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

}
